//获取url中的参数
function GetRequest() {  
    var url = location.search; //获取url中"?"符后的字串  
    var theRequest = new Object();  
    if (url.indexOf("?") != -1) {  
        var str = url.substr(1);  
        strs = str.split("&");  
        for (var i = 0; i < strs.length; i++) {  
            theRequest[strs[i].split("=")[0]] = (strs[i].split("=")[1]);  
        }  
    }  
    return theRequest;  
}  
//公用参数拼接
function paramsTrans(jsonObj) {
  jsonObj.userid = 	sessionStorage.getItem("userid");
  jsonObj.roleid = 	sessionStorage.getItem("roleid");
  return jsonObj
}

function httpGetRequest(url,params,callback){
	paramsTrans(params)
	$.ajax({
		type: "GET", //
		dataType: "JSON", //
		url: url,
		data: params,
		headers : {"token" : sessionStorage.getItem('token')},
		error: function (XMLHttpRequest, textStatus, errorThrown) {
			console.log(errorThrown)
			console.log(XMLHttpRequest)
			console.log(textStatus)
			alert("请求失败：" + textStatus);
		},
		success: function (msg){
			if(msg.code != -1 ){
				if(msg.code == 200){
					callback(msg);
				}else{
					alert(msg.msg);
				}
			}else{
				alert(msg.msg);
				window.parent.location.href = "/"
			}
		}
	});
}

function httpRequest(url,params,callback){
	paramsTrans(params)
	$.ajax({
			type: "POST", //
			dataType: "JSON", //
			url: url,
			data: params,
		    headers : {"token" : sessionStorage.getItem('token')},
			error: function (XMLHttpRequest, textStatus, errorThrown) { 
				console.log(errorThrown)
				console.log(XMLHttpRequest)
				console.log(textStatus)
				alert("请求失败：" + textStatus);
			},
			success: function (msg){
				if(msg.code != -1){
					if(msg.code == 200){
						callback(msg);
					}else{
						alert(msg.msg);
					}
				}else{
					alert(msg.msg);
					window.parent.location.replace("/")
				}

			}
	});
}



/** 
* 判断是否null 
* @param data 
*/
function isNull(data){ 
   return (data == "" || data == undefined || data == null) ? false : true; 
}
function checkRate(input) {
	var re = /^[0-9]+.?[0-9]*$/; //判断字符串是否为数字 //判断正整数 /^[1-9]+[0-9]*]*$/ 
	var nubmer = document.getElementById(input).value;

	if (!re.test(nubmer)) {
	return false;
	}
	return true;
}


//获取url中的参数
function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    var r = decodeURI(decodeURI(window.location.search)).substr(1).match(reg);  //匹配目标参数
    if (r != null) return unescape(r[2]); return null; //返回参数值
}
function getUrlParamPlus(name) {   
	   var url = location.search; //获取url中"?"符后的字串
	   if (url.indexOf("?") != -1) {    //判断是否有参数
	      var str = url.substr(1); //从第一个字符开始 因为第0个是?号 获取所有除问号的所有符串
	      strs = str.split("=");   //用等号进行分隔 （因为知道只有一个参数 所以直接用等号进分隔 如果有多个参数 要用&号分隔 再用等号进行分隔）
	   }
	   return strs[1];
	}
/**
 * 校验手机号
 */
function checkPhone(phoneNumber) {
  var myreg = /^[1][3,4,5,7,8,9][0-9]{9}$/;
  if (!myreg.test(phoneNumber)) {
    return false;
  }
  return true;
}

/**
 * 校验身份证号
 */
function checkIdno(idno){
   var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/; 
   if (reg.test(idno) === false) {
     return false;
   }  
   return true;
}

/**
 * 校验邮箱
 * @param email_address
 * @returns
 */
function isEmail(email_address){
  var regex = /^([0-9A-Za-z\-_\.]+)@([0-9a-z]+\.[a-z]{2,3}(\.[a-z]{2})?)$/g;
  if (regex.test(email_address)){
      return true;
  }else{
    return false;
  }
}

//定义serializeObject方法，序列化表单
$.fn.serializeObject = function() {
	var o = {};
	var a = this.serializeArray();
	$.each(a, function() {
		if (o[this.name]) {
			if (!o[this.name].push) {
				o[this.name] = [ o[this.name] ];
			}
			o[this.name].push(this.value || '');
		} else {
			o[this.name] = this.value || '';
		}
	});
	return o;
};


// List转成Tree
function toTreeData(data, attributes) {
	// resData 是从后端传过来的数据
	let resData = data;
	let tree = [];

	for (let i = 0; i < resData.length; i++) {
		// FatherID 是父级ID，对应自己的数据   rootID 是配置中的父级ID
		if (resData[i].ParentId === attributes['rootId']) {
			let obj = {
				id: resData[i][attributes.id],
				title: resData[i][attributes.name],
				spread: false,
				children: []
			};
			tree.push(obj);
			resData.splice(i, 1);
			i--;
		}
	}
	run(tree);

	function run(chiArr) {
		if (resData.length !== 0) {
			for (let i = 0; i < chiArr.length; i++) {
				for (let j = 0; j < resData.length; j++) {
					if (chiArr[i].id === resData[j][attributes.parentId]) {
						let obj = {
							id: resData[j][attributes.id],
							title: resData[j][attributes.name],
							spread: false,
							children: []
						};
						chiArr[i].children.push(obj);
						resData.splice(j, 1);
						j--;
					}
				}
				run(chiArr[i].children);
			}
		}
	}
	return tree;
}


// List转成Tree
function toMenuTreeData(data, attributes) {
	// resData 是从后端传过来的数据
	let resData = data;
	let tree = [];

	for (let i = 0; i < resData.length; i++) {
		// FatherID 是父级ID，对应自己的数据   rootID 是配置中的父级ID
		if (resData[i].ParentId === attributes['rootId']) {
			let obj = {
				id: resData[i][attributes.id],
				menutitle: resData[i][attributes.name],
				spread: false,
				url:resData[i][attributes.url],
				submenu: []
			};
			tree.push(obj);
			resData.splice(i, 1);
			i--;
		}
	}
	run(tree);

	function run(chiArr) {
		if (resData.length !== 0) {
			for (let i = 0; i < chiArr.length; i++) {
				for (let j = 0; j < resData.length; j++) {
					if (chiArr[i].id === resData[j][attributes.parentId]) {
						let obj = {
							id: resData[j][attributes.id],
							menutitle: resData[j][attributes.name],
							spread: false,
							url:resData[j][attributes.url],
							submenu: []
						};
						chiArr[i].submenu.push(obj);
						resData.splice(j, 1);
						j--;
					}
				}
				run(chiArr[i].submenu);
			}
		}
	}
	return tree;
}


